package com.hospital.drug.drugyan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hospital.drug.drugyan.dao.DrugMapper;
import com.hospital.drug.drugyan.entity.Drug_record;
import com.hospital.drug.drugyan.entity.Y_drugSelect;
import com.hospital.drug.drugyan.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 波——最帅的男人
 * @version 1.0
 * @date 2021/5/10 10:37
 */

@Service
public class DrugServiceImpl implements DrugService {

    @Autowired
    private DrugMapper drugMapper;

    /**
     * 分页查询
     *
     * @param pageNum
     * @param id
     * @param dateRiQi
     * @param dateRiQi2
     * @return
     */
    @Override
    public PageInfo<Y_drugSelect> pageListDrug(Integer pageNum, String id, String dateRiQi, String dateRiQi2) {
        PageHelper.startPage(pageNum, 5);
        List<Y_drugSelect> drugSingle = drugMapper.selDrugSingle(id, dateRiQi, dateRiQi2);
        PageInfo<Y_drugSelect> page = new PageInfo<>(drugSingle);
        return page;
    }

    /**
     * 根据id查询该患者具体开了多少药
     *
     * @param id
     * @return
     */
    @Override
    public List<Drug_record> selectAllDrug(Integer id, String date) {
        return drugMapper.selDrugs(id, date);
    }

    /**
     * 修改状态和药品数量
     *
     * @param id
     * @param date
     * @param arr1
     * @param arr2
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public boolean updStatus(Integer id, String date, String[] arr1, String[] arr2) {
        drugMapper.updStatus(id, date);
        for (int i = 0; i < arr1.length; i++) {
            drugMapper.updDrugCount(arr2[i], arr1[i]);
        }
        return true;
    }

    /**
     * 查询状态
     *
     * @param id   用户id
     * @param date
     * @return
     */
    @Override
    public int selStatus(Integer id, String date) {
        return drugMapper.selStatus(id, date);
    }

    @Override
    public String selectDrugCount(String[] arr1) {
        String str = "";
        for (int i = 0; i < arr1.length; i++) {
            int count = drugMapper.selDrugCount(arr1[i]);
            if (count < 30) {
                str += "当前药品" + arr1[i] + "的库存不足！";
            }
        }
        if (str.equals("")) {
            return "药品充足!";
        }
        return str;
    }

}
